package com.rt.easyjava.common.utils.others;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SwitchLineCamel {

    /**
     * 字段转成属性
     * @param columnName
     * @return
     */
    public static String switch2Property(String columnName){
        if(StringUtils.isNull(columnName))return columnName;
        if (columnName.contains("_")){
            Pattern pattern = Pattern.compile("_[a-z,0-9]");
            Matcher matcher = pattern.matcher(columnName);
            while(matcher.find()){
                String old = matcher.group();
                String new1 = matcher.group().toUpperCase();
                columnName = columnName.replaceAll(old, new1.replace("_",""));
            }
        }
        return columnName;
    }


    /**
     * 属性转字段
     * @param property
     * @return
     */
    /*public static String switch2Column(String property){
        if (property.matches("[a-z]*[A-Z]*[a-z]*[0-9]*[a-z]+[A-Z][a-z]+([A-Z][a-z]+)*")) {
            Pattern pattern = Pattern.compile("[A-Z]");
            Matcher matcher = pattern.matcher(property);
            while(matcher.find()){
                String old = matcher.group();
                String ne = matcher.group().toLowerCase();
                property = property.replaceAll(old, "_"+ne);
            }
        }
        return property;
    }*/

    /**
     * 驼峰转下划线
     * @param humpString
     * created by hbd 20160722
     * @return
     */
    public static String switch2Column(String humpString) {
        String regexStr = "[A-Z]";
        Matcher matcher = Pattern.compile(regexStr).matcher(humpString);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String g = matcher.group();
            matcher.appendReplacement(sb, "_" + g.toLowerCase());
        }
        matcher.appendTail(sb);
        if (sb.charAt(0) == '_') {
            sb.delete(0, 1);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(switch2Column("1Aa2deptId"));
        System.out.println(switch2Property("a2dept_id"));
    }
}
